<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>条款41-48 模板和泛型编程 | ShuangChenYue</title>
    <meta name="generator" content="VuePress 1.9.10">
    <link rel="icon" href="https://cdn.jsdelivr.net/gh/cmty256/imgs-blog@main/logo/白云.38zbldnhh180.jpg">
    <meta name="description" content="满招损，谦受益">
    <meta name="keywords" content="专注于Cpp语言的旅行者">
    
    <link rel="preload" href="/assets/css/0.styles.952d6952.css" as="style"><link rel="preload" href="/assets/js/app.67adcfd9.js" as="script"><link rel="preload" href="/assets/js/4.9aaa1650.js" as="script"><link rel="preload" href="/assets/js/1.5474518c.js" as="script"><link rel="preload" href="/assets/js/3.593d14fc.js" as="script"><link rel="preload" href="/assets/js/58.aad57f31.js" as="script"><link rel="prefetch" href="/assets/js/10.3242746b.js"><link rel="prefetch" href="/assets/js/100.9224de43.js"><link rel="prefetch" href="/assets/js/101.f0d1b059.js"><link rel="prefetch" href="/assets/js/102.996bfc6d.js"><link rel="prefetch" href="/assets/js/103.9bfdbd6f.js"><link rel="prefetch" href="/assets/js/104.8613f283.js"><link rel="prefetch" href="/assets/js/105.aa6e809e.js"><link rel="prefetch" href="/assets/js/106.90192392.js"><link rel="prefetch" href="/assets/js/107.e82a40b7.js"><link rel="prefetch" href="/assets/js/108.994cd438.js"><link rel="prefetch" href="/assets/js/109.ec15acc2.js"><link rel="prefetch" href="/assets/js/11.c04b41c1.js"><link rel="prefetch" href="/assets/js/110.c32d8576.js"><link rel="prefetch" href="/assets/js/111.453b5d50.js"><link rel="prefetch" href="/assets/js/112.ffbdb3a4.js"><link rel="prefetch" href="/assets/js/113.12b8ad7d.js"><link rel="prefetch" href="/assets/js/114.899d2998.js"><link rel="prefetch" href="/assets/js/115.b7ad9576.js"><link rel="prefetch" href="/assets/js/116.a8394748.js"><link rel="prefetch" href="/assets/js/117.0edfe25b.js"><link rel="prefetch" href="/assets/js/118.9161b1fe.js"><link rel="prefetch" href="/assets/js/119.be59e21b.js"><link rel="prefetch" href="/assets/js/12.41437bf6.js"><link rel="prefetch" href="/assets/js/120.bcf439fb.js"><link rel="prefetch" href="/assets/js/121.c3d251b8.js"><link rel="prefetch" href="/assets/js/122.62b1caba.js"><link rel="prefetch" href="/assets/js/123.787c2ab0.js"><link rel="prefetch" href="/assets/js/124.a880746f.js"><link rel="prefetch" href="/assets/js/125.d8edfe7b.js"><link rel="prefetch" href="/assets/js/126.4ff01546.js"><link rel="prefetch" href="/assets/js/127.9416d1ff.js"><link rel="prefetch" href="/assets/js/128.01a4a7a0.js"><link rel="prefetch" href="/assets/js/129.76876665.js"><link rel="prefetch" href="/assets/js/13.922328e9.js"><link rel="prefetch" href="/assets/js/130.7f631dd9.js"><link rel="prefetch" href="/assets/js/131.c9e0fde9.js"><link rel="prefetch" href="/assets/js/132.1c04cde5.js"><link rel="prefetch" href="/assets/js/133.e8f381cd.js"><link rel="prefetch" href="/assets/js/134.03d19f8b.js"><link rel="prefetch" href="/assets/js/135.44607494.js"><link rel="prefetch" href="/assets/js/136.6a1eb3c9.js"><link rel="prefetch" href="/assets/js/137.27898fd1.js"><link rel="prefetch" href="/assets/js/138.5bc0cf54.js"><link rel="prefetch" href="/assets/js/139.c2d1addc.js"><link rel="prefetch" href="/assets/js/14.e54d7526.js"><link rel="prefetch" href="/assets/js/140.052ec8e4.js"><link rel="prefetch" href="/assets/js/141.131abb5a.js"><link rel="prefetch" href="/assets/js/142.6ba6c07b.js"><link rel="prefetch" href="/assets/js/143.5dd51d22.js"><link rel="prefetch" href="/assets/js/144.b45afca8.js"><link rel="prefetch" href="/assets/js/145.faa9fb04.js"><link rel="prefetch" href="/assets/js/146.b54c024d.js"><link rel="prefetch" href="/assets/js/147.a1223242.js"><link rel="prefetch" href="/assets/js/148.4767bcb2.js"><link rel="prefetch" href="/assets/js/149.b65ab046.js"><link rel="prefetch" href="/assets/js/15.7082a3da.js"><link rel="prefetch" href="/assets/js/150.9bd8c175.js"><link rel="prefetch" href="/assets/js/151.9f830e96.js"><link rel="prefetch" href="/assets/js/152.41cde7f0.js"><link rel="prefetch" href="/assets/js/153.f57d65e0.js"><link rel="prefetch" href="/assets/js/154.5d7c8d51.js"><link rel="prefetch" href="/assets/js/155.0ae99532.js"><link rel="prefetch" href="/assets/js/156.5a54e043.js"><link rel="prefetch" href="/assets/js/157.c25b5d40.js"><link rel="prefetch" href="/assets/js/158.aa025b46.js"><link rel="prefetch" href="/assets/js/159.47939d88.js"><link rel="prefetch" href="/assets/js/16.fc775b7b.js"><link rel="prefetch" href="/assets/js/160.f8624459.js"><link rel="prefetch" href="/assets/js/161.7a075dc2.js"><link rel="prefetch" href="/assets/js/162.1d48f266.js"><link rel="prefetch" href="/assets/js/163.5d68a99f.js"><link rel="prefetch" href="/assets/js/164.1262d0e5.js"><link rel="prefetch" href="/assets/js/165.2ccf0bdd.js"><link rel="prefetch" href="/assets/js/166.21ece4d9.js"><link rel="prefetch" href="/assets/js/167.bf8adb95.js"><link rel="prefetch" href="/assets/js/168.1cb8440d.js"><link rel="prefetch" href="/assets/js/169.1dd1e396.js"><link rel="prefetch" href="/assets/js/17.ecc7be70.js"><link rel="prefetch" href="/assets/js/170.c29ec18f.js"><link rel="prefetch" href="/assets/js/171.38820827.js"><link rel="prefetch" href="/assets/js/172.bbc8ffc6.js"><link rel="prefetch" href="/assets/js/173.470e21e7.js"><link rel="prefetch" href="/assets/js/174.3c2df318.js"><link rel="prefetch" href="/assets/js/175.d2690cdb.js"><link rel="prefetch" href="/assets/js/176.9ca64696.js"><link rel="prefetch" href="/assets/js/177.76f3271d.js"><link rel="prefetch" href="/assets/js/178.d7d9def2.js"><link rel="prefetch" href="/assets/js/179.b5644743.js"><link rel="prefetch" href="/assets/js/18.31fe7ecd.js"><link rel="prefetch" href="/assets/js/180.7592d5ef.js"><link rel="prefetch" href="/assets/js/181.5cb77d35.js"><link rel="prefetch" href="/assets/js/182.6fa5633c.js"><link rel="prefetch" href="/assets/js/183.b3a53d1b.js"><link rel="prefetch" href="/assets/js/184.3815c537.js"><link rel="prefetch" href="/assets/js/185.bcf4ab71.js"><link rel="prefetch" href="/assets/js/186.1cc02f6d.js"><link rel="prefetch" href="/assets/js/187.8b425fb7.js"><link rel="prefetch" href="/assets/js/188.44ccbd02.js"><link rel="prefetch" href="/assets/js/189.353b35e3.js"><link rel="prefetch" href="/assets/js/19.520992d5.js"><link rel="prefetch" href="/assets/js/190.c284595f.js"><link rel="prefetch" href="/assets/js/191.788ecc2d.js"><link rel="prefetch" href="/assets/js/192.712a164e.js"><link rel="prefetch" href="/assets/js/193.da58aba3.js"><link rel="prefetch" href="/assets/js/194.6b1b1f4d.js"><link rel="prefetch" href="/assets/js/195.c31d5c39.js"><link rel="prefetch" href="/assets/js/196.f6670c4d.js"><link rel="prefetch" href="/assets/js/197.5a1f50ab.js"><link rel="prefetch" href="/assets/js/2.ab565158.js"><link rel="prefetch" href="/assets/js/20.69e29cdc.js"><link rel="prefetch" href="/assets/js/21.2fd424ad.js"><link rel="prefetch" href="/assets/js/22.d4c0be54.js"><link rel="prefetch" href="/assets/js/23.4bb90ecc.js"><link rel="prefetch" href="/assets/js/24.c01be6b2.js"><link rel="prefetch" href="/assets/js/25.c8833687.js"><link rel="prefetch" href="/assets/js/26.8042b555.js"><link rel="prefetch" href="/assets/js/27.0d5fa4c0.js"><link rel="prefetch" href="/assets/js/28.f9735b8b.js"><link rel="prefetch" href="/assets/js/29.3af53626.js"><link rel="prefetch" href="/assets/js/30.5f1b56d1.js"><link rel="prefetch" href="/assets/js/31.544b2649.js"><link rel="prefetch" href="/assets/js/32.aa321988.js"><link rel="prefetch" href="/assets/js/33.6aba2c86.js"><link rel="prefetch" href="/assets/js/34.e1bbff24.js"><link rel="prefetch" href="/assets/js/35.233f76e0.js"><link rel="prefetch" href="/assets/js/36.cb773972.js"><link rel="prefetch" href="/assets/js/37.393d9c59.js"><link rel="prefetch" href="/assets/js/38.e2d530c5.js"><link rel="prefetch" href="/assets/js/39.acaf1cc0.js"><link rel="prefetch" href="/assets/js/40.358f731e.js"><link rel="prefetch" href="/assets/js/41.ded24b7e.js"><link rel="prefetch" href="/assets/js/42.b9f683c3.js"><link rel="prefetch" href="/assets/js/43.c8fb3e66.js"><link rel="prefetch" href="/assets/js/44.633142da.js"><link rel="prefetch" href="/assets/js/45.6095e772.js"><link rel="prefetch" href="/assets/js/46.421d8c7a.js"><link rel="prefetch" href="/assets/js/47.da50fe47.js"><link rel="prefetch" href="/assets/js/48.15ff5726.js"><link rel="prefetch" href="/assets/js/49.b662e624.js"><link rel="prefetch" href="/assets/js/5.c1b8a209.js"><link rel="prefetch" href="/assets/js/50.a8bc75df.js"><link rel="prefetch" href="/assets/js/51.51e36ae7.js"><link rel="prefetch" href="/assets/js/52.54cc6e51.js"><link rel="prefetch" href="/assets/js/53.4173561d.js"><link rel="prefetch" href="/assets/js/54.7cab8416.js"><link rel="prefetch" href="/assets/js/55.3d7317d3.js"><link rel="prefetch" href="/assets/js/56.3c22255b.js"><link rel="prefetch" href="/assets/js/57.18e46e30.js"><link rel="prefetch" href="/assets/js/59.7897f6a7.js"><link rel="prefetch" href="/assets/js/6.3131f88a.js"><link rel="prefetch" href="/assets/js/60.5cd0051a.js"><link rel="prefetch" href="/assets/js/61.d9606403.js"><link rel="prefetch" href="/assets/js/62.aede9df0.js"><link rel="prefetch" href="/assets/js/63.2c30e554.js"><link rel="prefetch" href="/assets/js/64.18228ab7.js"><link rel="prefetch" href="/assets/js/65.27cb3fba.js"><link rel="prefetch" href="/assets/js/66.2fa6c2dc.js"><link rel="prefetch" href="/assets/js/67.d274a8df.js"><link rel="prefetch" href="/assets/js/68.3069cfcf.js"><link rel="prefetch" href="/assets/js/69.4c28600f.js"><link rel="prefetch" href="/assets/js/7.89e6165d.js"><link rel="prefetch" href="/assets/js/70.4175440c.js"><link rel="prefetch" href="/assets/js/71.2ee6b435.js"><link rel="prefetch" href="/assets/js/72.c75e3bb8.js"><link rel="prefetch" href="/assets/js/73.6f8b8211.js"><link rel="prefetch" href="/assets/js/74.6c7720cf.js"><link rel="prefetch" href="/assets/js/75.cccfb229.js"><link rel="prefetch" href="/assets/js/76.f022e5da.js"><link rel="prefetch" href="/assets/js/77.dab46206.js"><link rel="prefetch" href="/assets/js/78.ca574b2a.js"><link rel="prefetch" href="/assets/js/79.3d75e618.js"><link rel="prefetch" href="/assets/js/80.091749b1.js"><link rel="prefetch" href="/assets/js/81.14db0e21.js"><link rel="prefetch" href="/assets/js/82.8a2b1809.js"><link rel="prefetch" href="/assets/js/83.84a4b599.js"><link rel="prefetch" href="/assets/js/84.11d7c222.js"><link rel="prefetch" href="/assets/js/85.273d4388.js"><link rel="prefetch" href="/assets/js/86.fb40e20c.js"><link rel="prefetch" href="/assets/js/87.3316639e.js"><link rel="prefetch" href="/assets/js/88.dfc52200.js"><link rel="prefetch" href="/assets/js/89.8d615f6e.js"><link rel="prefetch" href="/assets/js/90.1d9f08bb.js"><link rel="prefetch" href="/assets/js/91.566813e7.js"><link rel="prefetch" href="/assets/js/92.d13c6f41.js"><link rel="prefetch" href="/assets/js/93.845c42a0.js"><link rel="prefetch" href="/assets/js/94.20a37b77.js"><link rel="prefetch" href="/assets/js/95.1a498005.js"><link rel="prefetch" href="/assets/js/96.39fa7f4b.js"><link rel="prefetch" href="/assets/js/97.50f7170e.js"><link rel="prefetch" href="/assets/js/98.dd2e15d6.js"><link rel="prefetch" href="/assets/js/99.ef7ea06a.js"><link rel="prefetch" href="/assets/js/vendors~docsearch.ae6b1de9.js">
    <link rel="stylesheet" href="/assets/css/0.styles.952d6952.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="https://cdn.jsdelivr.net/gh/cmty256/imgs-blog@main/logo/白云.38zbldnhh180.jpg" alt="ShuangChenYue" class="logo"> <span class="site-name can-hide">ShuangChenYue</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="CPP语言" class="dropdown-title"><!----> <span class="title" style="display:;">CPP语言</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/c5bdd8/" class="nav-link">Cpp之旅</a></li><li class="dropdown-item"><!----> <a href="/pages/279e62/" class="nav-link">Cpp专栏</a></li><li class="dropdown-item"><!----> <a href="/pages/801755/" class="nav-link">Effective_CPP</a></li><li class="dropdown-item"><!----> <a href="/pages/6b2468/" class="nav-link">muduo网络库</a></li><li class="dropdown-item"><!----> <a href="/pages/5f8c9f/" class="nav-link">Unix环境高级编程</a></li><li class="dropdown-item"><!----> <a href="/pages/3f1d21/" class="nav-link">Cpp提高编程</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="计算机基础" class="dropdown-title"><!----> <span class="title" style="display:;">计算机基础</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/7b1cb2/" class="nav-link">计算机网络</a></li><li class="dropdown-item"><!----> <a href="/pages/6048a8/" class="nav-link">操作系统</a></li><li class="dropdown-item"><!----> <a href="/pages/3b34ba/" class="nav-link">数据结构</a></li><li class="dropdown-item"><!----> <a href="/pages/412fe7/" class="nav-link">Linux</a></li><li class="dropdown-item"><!----> <a href="/pages/2dcfa1/" class="nav-link">算法</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="数据库" class="dropdown-title"><!----> <span class="title" style="display:;">数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/efa3f2/" class="nav-link">基础篇</a></li><li class="dropdown-item"><!----> <a href="/pages/ccc445/" class="nav-link">MySql</a></li><li class="dropdown-item"><!----> <a href="/pages/54616e/" class="nav-link">Redis</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="嵌入式软件开发" class="dropdown-title"><!----> <span class="title" style="display:;">嵌入式软件开发</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/d142c2/" class="nav-link">电子嵌入式通信协议</a></li><li class="dropdown-item"><!----> <a href="/pages/4c6bf3/" class="nav-link">深入浅出SSD</a></li><li class="dropdown-item"><!----> <a href="/pages/d3f36a/" class="nav-link">文件系统</a></li><li class="dropdown-item"><!----> <a href="/pages/e0cca7/" class="nav-link">汇编语言</a></li><li class="dropdown-item"><!----> <a href="/pages/fab2d7/" class="nav-link">STM32</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="开发日常" class="dropdown-title"><!----> <span class="title" style="display:;">开发日常</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/e472d1/" class="nav-link">随笔（持续更新）</a></li><li class="dropdown-item"><!----> <a href="/pages/71f6ae/" class="nav-link">Git知识总结</a></li><li class="dropdown-item"><!----> <a href="/pages/db6fb8/" class="nav-link">Git备忘清单</a></li><li class="dropdown-item"><!----> <a href="/pages/e1081f/" class="nav-link">Git 创建删除远程分支</a></li><li class="dropdown-item"><!----> <a href="/pages/777b8a/" class="nav-link">nvm使用小结</a></li><li class="dropdown-item"><!----> <a href="/pages/ee770e/" class="nav-link">虚拟机固定 IP 地址</a></li><li class="dropdown-item"><!----> <a href="/pages/1ab9a6/" class="nav-link">Shell 脚本学习笔记</a></li><li class="dropdown-item"><!----> <a href="/pages/411aa4/" class="nav-link">VScode 插件 CodeGeeX 使用教程</a></li><li class="dropdown-item"><!----> <a href="/pages/0d525d/" class="nav-link">KylinV10 将项目上传至 Github教程</a></li><li class="dropdown-item"><!----> <a href="/pages/907786/" class="nav-link">KylinV10 安装 MySQL 教程（可防踩雷）</a></li><li class="dropdown-item"><!----> <a href="/pages/a2d21e/" class="nav-link">kylinV10-SP1 安装 QT</a></li><li class="dropdown-item"><!----> <a href="/pages/b561cf/" class="nav-link">高并发内存池</a></li><li class="dropdown-item"><!----> <a href="/pages/6ab6d1/" class="nav-link">USBGUARD 项目编译环境配置</a></li><li class="dropdown-item"><!----> <a href="/pages/883f02/" class="nav-link">Power_Destory 项目</a></li><li class="dropdown-item"><!----> <a href="/pages/479472/" class="nav-link">U 盘清除工具编译教程</a></li><li class="dropdown-item"><!----> <a href="/pages/9c4241/" class="nav-link">个人博客代码推送教程</a></li><li class="dropdown-item"><!----> <a href="/pages/3ad765/" class="nav-link">SVN Trunk Branches的Merge操作</a></li><li class="dropdown-item"><!----> <a href="/pages/0c0ca8/" class="nav-link">如何高效阅读嵌入式项目代码</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端学习" class="dropdown-title"><!----> <span class="title" style="display:;">前端学习</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/99897f/" class="nav-link">HTML与CSS</a></li><li class="dropdown-item"><!----> <a href="/pages/51542d/" class="nav-link">JS学习</a></li><li class="dropdown-item"><!----> <a href="/pages/803f9d/" class="nav-link">Vue3入门</a></li><li class="dropdown-item"><!----> <a href="/pages/ca4cfb/" class="nav-link">Vue3进阶</a></li><li class="dropdown-item"><!----> <a href="/pages/50e8d3/" class="nav-link">黑马Vue3</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="MFC" class="dropdown-title"><!----> <span class="title" style="display:;">MFC</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/a4b108/" class="nav-link">MFC编程随记</a></li><li class="dropdown-item"><!----> <a href="/pages/41acbd/" class="nav-link">MFC实现ini配置文件的读取</a></li><li class="dropdown-item"><!----> <a href="/pages/951a7a/" class="nav-link">MFC实现点击列表头排序</a></li><li class="dropdown-item"><!----> <a href="/pages/a8598f/" class="nav-link">贴图法美化Button按钮</a></li><li class="dropdown-item"><!----> <a href="/pages/054516/" class="nav-link">MFC使用细节</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="闪存" class="dropdown-title"><!----> <span class="title" style="display:;">闪存</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/b925b8/" class="nav-link">如何高效阅读嵌入式项目代码</a></li><li class="dropdown-item"><!----> <a href="/pages/28ec23/" class="nav-link">NAND Flash</a></li><li class="dropdown-item"><!----> <a href="/pages/62bf40/" class="nav-link">ARM 处理器</a></li><li class="dropdown-item"><!----> <a href="/pages/1a9374/" class="nav-link">嵌入式基础知识-存储器</a></li><li class="dropdown-item"><!----> <a href="/pages/aac5e3/" class="nav-link">闪存存储和制造技术概述</a></li><li class="dropdown-item"><!----> <a href="/pages/8f6056/" class="nav-link">芯片IO驱动力</a></li><li class="dropdown-item"><!----> <a href="/pages/d146b8/" class="nav-link">主流先进封装技术介绍</a></li><li class="dropdown-item"><!----> <a href="/pages/16f0ba/" class="nav-link">NAND Flash基础</a></li><li class="dropdown-item"><!----> <a href="/pages/90d8d0/" class="nav-link">基于PA算法的FTL引导</a></li><li class="dropdown-item"><!----> <a href="/pages/eb672b/" class="nav-link">SD逻辑擦除和物理擦除</a></li><li class="dropdown-item"><!----> <a href="/pages/747121/" class="nav-link">NAND Flash的SDR、ONFI、DDR接口</a></li><li class="dropdown-item"><!----> <a href="/pages/1eb351/" class="nav-link">【详解】Nand Flash必看知识</a></li><li class="dropdown-item"><!----> <a href="/pages/d2512a/" class="nav-link">【两万字详解】Nand Flash必看知识</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="面经" class="dropdown-title"><!----> <span class="title" style="display:;">面经</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/d69946/" class="nav-link">虎牙C++技术面经</a></li><li class="dropdown-item"><!----> <a href="/pages/29251d/" class="nav-link">金山一面复习</a></li><li class="dropdown-item"><!----> <a href="/pages/c7c01f/" class="nav-link">完美世界秋招 C++ 游戏开发面经(Cpp部分)</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="其它" class="dropdown-title"><!----> <span class="title" style="display:;">其它</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/fa256e/" class="nav-link">博客搭建</a></li><li class="dropdown-item"><!----> <a href="/pages/335531/" class="nav-link">网站收藏箱</a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><!----> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="CPP语言" class="dropdown-title"><!----> <span class="title" style="display:;">CPP语言</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/c5bdd8/" class="nav-link">Cpp之旅</a></li><li class="dropdown-item"><!----> <a href="/pages/279e62/" class="nav-link">Cpp专栏</a></li><li class="dropdown-item"><!----> <a href="/pages/801755/" class="nav-link">Effective_CPP</a></li><li class="dropdown-item"><!----> <a href="/pages/6b2468/" class="nav-link">muduo网络库</a></li><li class="dropdown-item"><!----> <a href="/pages/5f8c9f/" class="nav-link">Unix环境高级编程</a></li><li class="dropdown-item"><!----> <a href="/pages/3f1d21/" class="nav-link">Cpp提高编程</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="计算机基础" class="dropdown-title"><!----> <span class="title" style="display:;">计算机基础</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/7b1cb2/" class="nav-link">计算机网络</a></li><li class="dropdown-item"><!----> <a href="/pages/6048a8/" class="nav-link">操作系统</a></li><li class="dropdown-item"><!----> <a href="/pages/3b34ba/" class="nav-link">数据结构</a></li><li class="dropdown-item"><!----> <a href="/pages/412fe7/" class="nav-link">Linux</a></li><li class="dropdown-item"><!----> <a href="/pages/2dcfa1/" class="nav-link">算法</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="数据库" class="dropdown-title"><!----> <span class="title" style="display:;">数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/efa3f2/" class="nav-link">基础篇</a></li><li class="dropdown-item"><!----> <a href="/pages/ccc445/" class="nav-link">MySql</a></li><li class="dropdown-item"><!----> <a href="/pages/54616e/" class="nav-link">Redis</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="嵌入式软件开发" class="dropdown-title"><!----> <span class="title" style="display:;">嵌入式软件开发</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/d142c2/" class="nav-link">电子嵌入式通信协议</a></li><li class="dropdown-item"><!----> <a href="/pages/4c6bf3/" class="nav-link">深入浅出SSD</a></li><li class="dropdown-item"><!----> <a href="/pages/d3f36a/" class="nav-link">文件系统</a></li><li class="dropdown-item"><!----> <a href="/pages/e0cca7/" class="nav-link">汇编语言</a></li><li class="dropdown-item"><!----> <a href="/pages/fab2d7/" class="nav-link">STM32</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="开发日常" class="dropdown-title"><!----> <span class="title" style="display:;">开发日常</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/e472d1/" class="nav-link">随笔（持续更新）</a></li><li class="dropdown-item"><!----> <a href="/pages/71f6ae/" class="nav-link">Git知识总结</a></li><li class="dropdown-item"><!----> <a href="/pages/db6fb8/" class="nav-link">Git备忘清单</a></li><li class="dropdown-item"><!----> <a href="/pages/e1081f/" class="nav-link">Git 创建删除远程分支</a></li><li class="dropdown-item"><!----> <a href="/pages/777b8a/" class="nav-link">nvm使用小结</a></li><li class="dropdown-item"><!----> <a href="/pages/ee770e/" class="nav-link">虚拟机固定 IP 地址</a></li><li class="dropdown-item"><!----> <a href="/pages/1ab9a6/" class="nav-link">Shell 脚本学习笔记</a></li><li class="dropdown-item"><!----> <a href="/pages/411aa4/" class="nav-link">VScode 插件 CodeGeeX 使用教程</a></li><li class="dropdown-item"><!----> <a href="/pages/0d525d/" class="nav-link">KylinV10 将项目上传至 Github教程</a></li><li class="dropdown-item"><!----> <a href="/pages/907786/" class="nav-link">KylinV10 安装 MySQL 教程（可防踩雷）</a></li><li class="dropdown-item"><!----> <a href="/pages/a2d21e/" class="nav-link">kylinV10-SP1 安装 QT</a></li><li class="dropdown-item"><!----> <a href="/pages/b561cf/" class="nav-link">高并发内存池</a></li><li class="dropdown-item"><!----> <a href="/pages/6ab6d1/" class="nav-link">USBGUARD 项目编译环境配置</a></li><li class="dropdown-item"><!----> <a href="/pages/883f02/" class="nav-link">Power_Destory 项目</a></li><li class="dropdown-item"><!----> <a href="/pages/479472/" class="nav-link">U 盘清除工具编译教程</a></li><li class="dropdown-item"><!----> <a href="/pages/9c4241/" class="nav-link">个人博客代码推送教程</a></li><li class="dropdown-item"><!----> <a href="/pages/3ad765/" class="nav-link">SVN Trunk Branches的Merge操作</a></li><li class="dropdown-item"><!----> <a href="/pages/0c0ca8/" class="nav-link">如何高效阅读嵌入式项目代码</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端学习" class="dropdown-title"><!----> <span class="title" style="display:;">前端学习</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/99897f/" class="nav-link">HTML与CSS</a></li><li class="dropdown-item"><!----> <a href="/pages/51542d/" class="nav-link">JS学习</a></li><li class="dropdown-item"><!----> <a href="/pages/803f9d/" class="nav-link">Vue3入门</a></li><li class="dropdown-item"><!----> <a href="/pages/ca4cfb/" class="nav-link">Vue3进阶</a></li><li class="dropdown-item"><!----> <a href="/pages/50e8d3/" class="nav-link">黑马Vue3</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="MFC" class="dropdown-title"><!----> <span class="title" style="display:;">MFC</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/a4b108/" class="nav-link">MFC编程随记</a></li><li class="dropdown-item"><!----> <a href="/pages/41acbd/" class="nav-link">MFC实现ini配置文件的读取</a></li><li class="dropdown-item"><!----> <a href="/pages/951a7a/" class="nav-link">MFC实现点击列表头排序</a></li><li class="dropdown-item"><!----> <a href="/pages/a8598f/" class="nav-link">贴图法美化Button按钮</a></li><li class="dropdown-item"><!----> <a href="/pages/054516/" class="nav-link">MFC使用细节</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="闪存" class="dropdown-title"><!----> <span class="title" style="display:;">闪存</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/b925b8/" class="nav-link">如何高效阅读嵌入式项目代码</a></li><li class="dropdown-item"><!----> <a href="/pages/28ec23/" class="nav-link">NAND Flash</a></li><li class="dropdown-item"><!----> <a href="/pages/62bf40/" class="nav-link">ARM 处理器</a></li><li class="dropdown-item"><!----> <a href="/pages/1a9374/" class="nav-link">嵌入式基础知识-存储器</a></li><li class="dropdown-item"><!----> <a href="/pages/aac5e3/" class="nav-link">闪存存储和制造技术概述</a></li><li class="dropdown-item"><!----> <a href="/pages/8f6056/" class="nav-link">芯片IO驱动力</a></li><li class="dropdown-item"><!----> <a href="/pages/d146b8/" class="nav-link">主流先进封装技术介绍</a></li><li class="dropdown-item"><!----> <a href="/pages/16f0ba/" class="nav-link">NAND Flash基础</a></li><li class="dropdown-item"><!----> <a href="/pages/90d8d0/" class="nav-link">基于PA算法的FTL引导</a></li><li class="dropdown-item"><!----> <a href="/pages/eb672b/" class="nav-link">SD逻辑擦除和物理擦除</a></li><li class="dropdown-item"><!----> <a href="/pages/747121/" class="nav-link">NAND Flash的SDR、ONFI、DDR接口</a></li><li class="dropdown-item"><!----> <a href="/pages/1eb351/" class="nav-link">【详解】Nand Flash必看知识</a></li><li class="dropdown-item"><!----> <a href="/pages/d2512a/" class="nav-link">【两万字详解】Nand Flash必看知识</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="面经" class="dropdown-title"><!----> <span class="title" style="display:;">面经</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/d69946/" class="nav-link">虎牙C++技术面经</a></li><li class="dropdown-item"><!----> <a href="/pages/29251d/" class="nav-link">金山一面复习</a></li><li class="dropdown-item"><!----> <a href="/pages/c7c01f/" class="nav-link">完美世界秋招 C++ 游戏开发面经(Cpp部分)</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="其它" class="dropdown-title"><!----> <span class="title" style="display:;">其它</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/fa256e/" class="nav-link">博客搭建</a></li><li class="dropdown-item"><!----> <a href="/pages/335531/" class="nav-link">网站收藏箱</a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Cpp之旅</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Cpp专栏</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>Effetcive_CPP</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/801755/" class="sidebar-link">条款1-4 让自己习惯Cpp</a></li><li><a href="/pages/4fc2cb/" class="sidebar-link">条款5-12 构造析构赋值运算</a></li><li><a href="/pages/ec62b4/" class="sidebar-link">条款26-31 实现</a></li><li><a href="/pages/b4a7fb/" class="sidebar-link">条款32-40 继承与面向对象设计</a></li><li><a href="/pages/799b40/" aria-current="page" class="active sidebar-link">条款41-48 模板和泛型编程</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/pages/799b40/#_1、存在的问题" class="sidebar-link">1、存在的问题：</a></li><li class="sidebar-sub-header level2"><a href="/pages/799b40/#_2、解决方法" class="sidebar-link">2、解决方法：</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/799b40/#a-使用this" class="sidebar-link">a）使用this-&gt;</a></li><li class="sidebar-sub-header level3"><a href="/pages/799b40/#b-使用using声明" class="sidebar-link">b） 使用using声明</a></li><li class="sidebar-sub-header level3"><a href="/pages/799b40/#c-指明被调函数所在类-基类资格修饰符" class="sidebar-link">C） 指明被调函数所在类：基类资格修饰符</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/799b40/#一、添加成员函数模板" class="sidebar-link">一、添加成员函数模板</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/799b40/#以一个例子引出-何时设计成员函数模板" class="sidebar-link">以一个例子引出，何时设计成员函数模板</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/799b40/#二、约束成员函数模板的行为" class="sidebar-link">二、约束成员函数模板的行为</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/799b40/#解决方法" class="sidebar-link">解决方法</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/799b40/#三、设计赋值成员函数模板" class="sidebar-link">三、设计赋值成员函数模板</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/799b40/#演示说明" class="sidebar-link">演示说明</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/799b40/#四、与默认函数的区别" class="sidebar-link">四、与默认函数的区别</a></li><li class="sidebar-sub-header level2"><a href="/pages/799b40/#五、总结" class="sidebar-link">五、总结</a></li><li class="sidebar-sub-header level2"><a href="/pages/799b40/#_1、如果想写一个将迭代器移动d单位的函数" class="sidebar-link">1、如果想写一个将迭代器移动d单位的函数</a></li><li class="sidebar-sub-header level2"><a href="/pages/799b40/#_2、stl迭代器一共有5类" class="sidebar-link">2、STL迭代器一共有5类</a></li><li class="sidebar-sub-header level2"><a href="/pages/799b40/#_3、解决方式就是利用iterator-traits" class="sidebar-link">3、解决方式就是利用iterator_traits</a></li><li class="sidebar-sub-header level2"><a href="/pages/799b40/#_1、内容" class="sidebar-link">1、内容</a></li><li class="sidebar-sub-header level2"><a href="/pages/799b40/#_2、-总结" class="sidebar-link">2、 总结</a></li></ul></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>muduo网络库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Unix环境高级编程</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Cpp提高编程</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><span data-v-06225672>CPP语言</span></li><li data-v-06225672><span data-v-06225672>Effetcive_CPP</span></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="javascript:;" data-v-06225672>霜晨月</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2023-11-24</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">条款41-48 模板和泛型编程<!----></h1> <!----> <div class="theme-vdoing-content content__default"><h1 id="模板和泛型编程"><a href="#模板和泛型编程" class="header-anchor">#</a> 模板和泛型编程</h1> <h1 id="条款41-了解隐式接口和编译期多态"><a href="#条款41-了解隐式接口和编译期多态" class="header-anchor">#</a> 条款41：了解隐式接口和编译期多态</h1> <p>理解条款41的关键点如下：</p> <ol><li><p>隐式接口：隐式接口是指一个类或对象所提供的所有可用的成员函数和非成员函数，包括非虚函数、虚函数和重载函数。通过这些函数，对象向外部提供了一种使用的方式，形成了其隐式接口。</p></li> <li><p>编译期多态：编译期多态是通过函数重载和模板技术来实现的。在编译期间，**编译器可以根据函数的参数类型和模板实例化的类型进行静态类型推导和函数匹配，从而选择调用合适的函数。**这种多态性在编译期间解决，不涉及运行时的动态绑定。</p></li> <li><p>隐式接口和编译期多态的优势：理解隐式接口和编译期多态的概念可以提供更好的代码复用、可扩展性和可维护性。通过隐式接口，可以在不修改类定义的情况下添加新的功能和行为。通过编译期多态，可以在编译期间解决函数的调用问题，提高性能并避免运行时的开销。</p></li> <li><p>使用隐式接口和编译期多态：为了有效地利用隐式接口和编译期多态，应该遵循一些准则：</p> <ul><li>尽量使用非成员函数，将算法和数据分离。</li> <li>使用函数重载来处理不同参数类型的操作。</li> <li>使用模板来实现通用的算法和数据结构。</li></ul></li></ol> <h1 id="条款42-了解typename的双重意义"><a href="#条款42-了解typename的双重意义" class="header-anchor">#</a> 条款42：了解typename的双重意义</h1> <p>理解条款42的关键点如下：</p> <ol><li><p><code>typename</code>关键字的第一层意义：在Cpp模板中，当使用一个嵌套从属类型（nested dependent type）时，编译器无法确定这个从属类型是一个类型名（type name）还是一个静态成员变量（static member variable）。此时，需要使用<code>typename</code>关键字来明确告诉编译器这个从属类型是一个类型名。</p></li> <li><p><code>typename</code>关键字的第二层意义：在Cpp中，当定义模板的类型参数时，使用<code>typename</code>关键字来指定该参数是一个类型名。</p></li> <li><p><code>typename</code>的正确使用方式：在模板中，当使用从属类型时，如果编译器无法推断出这是一个类型名，需要使用<code>typename</code>关键字。例如：</p></li></ol> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">T</span><span class="token operator">&gt;</span>
<span class="token keyword">void</span> <span class="token function">foo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">typename</span> <span class="token class-name">T</span><span class="token double-colon punctuation">::</span>NestedType<span class="token operator">*</span> ptr<span class="token punctuation">;</span> <span class="token comment">// 使用typename指定T::NestedType是一个类型名</span>
    <span class="token comment">// 其他代码...</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><ol start="4"><li><code>typename</code>关键字的限制：<code>typename</code>关键字只能在模板中使用，用于指定从属类型是一个类型名。在非模板代码中，不需要使用<code>typename</code>关键字。</li></ol> <p>==<strong>请记住：</strong>==</p> <ol><li>==<strong>声明template参数时，前缀关键字class和typename可互换。</strong>==</li> <li>==<strong>请使用关键字typename标识嵌套从属类型名称；但不得在base class lists（基类列）或member initialization list（成员初值列）内以它作为base class 修饰符。</strong>==</li></ol> <h1 id="条款43-学习处理模板化基类内的名称"><a href="#条款43-学习处理模板化基类内的名称" class="header-anchor">#</a> 条款43：学习处理模板化基类内的名称</h1> <h2 id="_1、存在的问题"><a href="#_1、存在的问题" class="header-anchor">#</a> <strong>1、存在的问题：</strong></h2> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">T1</span><span class="token operator">&gt;</span>
<span class="token keyword">class</span> <span class="token class-name">Class_A</span><span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
	<span class="token keyword">void</span> <span class="token function">send1</span><span class="token punctuation">(</span>T1 var<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">T1</span><span class="token operator">&gt;</span>
<span class="token keyword">class</span> <span class="token class-name">Class_B</span><span class="token operator">:</span><span class="token base-clause"><span class="token keyword">public</span> <span class="token class-name">Class_A</span><span class="token operator">&lt;</span> <span class="token class-name">T1</span> <span class="token operator">&gt;</span></span><span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
	<span class="token keyword">void</span> <span class="token function">send2</span><span class="token punctuation">(</span>T1 var<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">send1</span><span class="token punctuation">(</span>var<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//编译错误，调用模板基类内函数失败</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>与普通继承的差别：==<strong>在一般继承体系中，派生类可以调用基类方法，但模板继承中存在问题。</strong>==</p> <p>原因：==<strong>由于base class template可能被特化，而特化版本可能不提供和一般性template模板相同的接口。因此编译器往往拒绝在templated base class（模板基类）内寻找继承而来的名称</strong>==</p> <p>例子：</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token operator">&gt;</span>
<span class="token keyword">class</span> <span class="token class-name">Class_A</span><span class="token operator">&lt;</span>Type_1<span class="token operator">&gt;</span><span class="token punctuation">{</span> <span class="token comment">//特化模板类，类型为Type_1时的模板类</span>
<span class="token keyword">public</span><span class="token operator">:</span>
	<span class="token keyword">void</span> <span class="token function">send3</span><span class="token punctuation">(</span>Type_1 var<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 该特化类中不存在send1函数</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>知识点：<code>template&lt;&gt;</code>为特化模板标志
现象：由于特化类中不存在<code>send1</code>函数，因此派生类调用该函数失败**==。所以编译器由于知道基类模板可能被特化，使得接口不一致，所以禁止了这样的调用。==**</p> <h2 id="_2、解决方法"><a href="#_2、解决方法" class="header-anchor">#</a> <strong>2、解决方法：</strong></h2> <p>==解决方法的原理：对编译器承诺，base class template的任何版本（包括特化版本）都将支持一般版本所提供的的接口。==</p> <h3 id="a-使用this"><a href="#a-使用this" class="header-anchor">#</a> a）使用this-&gt;</h3> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">T1</span><span class="token operator">&gt;</span>
<span class="token keyword">class</span> <span class="token class-name">Class_B</span><span class="token operator">:</span><span class="token base-clause"><span class="token keyword">public</span> <span class="token class-name">Class_A</span><span class="token operator">&lt;</span> <span class="token class-name">T1</span> <span class="token operator">&gt;</span></span><span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
	<span class="token keyword">void</span> <span class="token function">send2</span><span class="token punctuation">(</span>T1 var<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token operator">-&gt;</span><span class="token function">send1</span><span class="token punctuation">(</span>var<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>  <span class="token comment">// 告诉编译器，假设send1被继承</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="b-使用using声明"><a href="#b-使用using声明" class="header-anchor">#</a> b） 使用using声明</h3> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">T1</span><span class="token operator">&gt;</span>
<span class="token keyword">class</span> <span class="token class-name">Class_B</span><span class="token operator">:</span><span class="token base-clause"><span class="token keyword">public</span> <span class="token class-name">Class_A</span><span class="token operator">&lt;</span> <span class="token class-name">T1</span> <span class="token operator">&gt;</span></span><span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
	<span class="token keyword">using</span> Class_A<span class="token operator">&lt;</span> T1 <span class="token operator">&gt;</span><span class="token double-colon punctuation">::</span>send1<span class="token punctuation">;</span> <span class="token comment">// 告诉编译器，send1在基类中</span>
	<span class="token keyword">void</span> <span class="token function">send2</span><span class="token punctuation">(</span>T1 var<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">send1</span><span class="token punctuation">(</span>var<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>  
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="c-指明被调函数所在类-基类资格修饰符"><a href="#c-指明被调函数所在类-基类资格修饰符" class="header-anchor">#</a> C） 指明被调函数所在类：基类资格修饰符</h3> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">T1</span><span class="token operator">&gt;</span>
<span class="token keyword">class</span> <span class="token class-name">Class_B</span><span class="token operator">:</span><span class="token base-clause"><span class="token keyword">public</span> <span class="token class-name">Class_A</span><span class="token operator">&lt;</span> <span class="token class-name">T1</span> <span class="token operator">&gt;</span></span><span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
	<span class="token keyword">void</span> <span class="token function">send2</span><span class="token punctuation">(</span>T1 var<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">Class_A</span><span class="token operator">&lt;</span> T1 <span class="token operator">&gt;</span><span class="token double-colon punctuation">::</span><span class="token function">send1</span><span class="token punctuation">(</span>var<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>  <span class="token comment">// 告诉编译器，send1在基类中</span>
	<span class="token comment">// 此方法不好若send1为虚函数，则Class_A&lt; T1 &gt;::修饰会关闭virtual绑定行为</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><strong>==请记住：==</strong></p> <ol><li><strong>==可在derived class templates内通过“this-&gt;”指涉base class template内的成员名称或藉由一个明白写出的“base class 资格修饰符”完成。==</strong></li></ol> <h1 id="条款44-将与参数无关的代码抽离templates"><a href="#条款44-将与参数无关的代码抽离templates" class="header-anchor">#</a> 条款44：将与参数无关的代码抽离templates</h1> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">T</span><span class="token punctuation">,</span>std<span class="token double-colon punctuation">::</span>size_t n<span class="token operator">&gt;</span>
<span class="token keyword">class</span> <span class="token class-name">SquareMatrix</span><span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
    <span class="token keyword">void</span> <span class="token function">invert</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>现在，考虑这些代码：</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code>SquareMatrix<span class="token operator">&lt;</span><span class="token keyword">double</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span> sm1<span class="token punctuation">;</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
sm1<span class="token punctuation">.</span><span class="token function">invert</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

SquareMatrix<span class="token operator">&lt;</span><span class="token keyword">double</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token operator">&gt;</span> sm2<span class="token punctuation">;</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
sm2<span class="token punctuation">.</span><span class="token function">invert</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>这样将会生成两份invert，这些函数并非完全相同，因为其中一个操作的是5<em>5矩阵而另一个是10</em>10的矩阵，但除了常量5和10，两个函数的其他部分完全相同，这是templates引出代码膨胀的一个典型例子。</p> <p>可以发现这两个函数完全相同，只除了一个使用5而另一个使用10，那你会怎么做？以5和10来调用这个带参数的函数，而不重复代码。下面我们来进行第一次对SquareMatrix的修改：</p> <p><strong>策略1：</strong></p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">T</span><span class="token operator">&gt;</span>
<span class="token keyword">class</span> <span class="token class-name">SquareMatrixBase</span><span class="token punctuation">{</span>
<span class="token keyword">protected</span><span class="token operator">:</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
    <span class="token keyword">void</span> <span class="token function">invert</span><span class="token punctuation">(</span>std<span class="token double-colon punctuation">::</span>size_t matrixSize<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">T</span><span class="token punctuation">,</span>std<span class="token double-colon punctuation">::</span>size_t n<span class="token operator">&gt;</span>
<span class="token keyword">class</span> <span class="token class-name">SquareMatrix</span><span class="token operator">:</span><span class="token base-clause"><span class="token keyword">private</span> <span class="token class-name">SquareMatrixBase</span><span class="token operator">&lt;</span><span class="token class-name">T</span><span class="token operator">&gt;</span></span><span class="token punctuation">{</span>
<span class="token keyword">private</span><span class="token operator">:</span>
    <span class="token keyword">using</span> SquareMatrixBase<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span><span class="token double-colon punctuation">::</span>invert<span class="token punctuation">;</span>

<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
    <span class="token keyword">void</span> <span class="token function">invert</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token operator">-&gt;</span><span class="token function">invert</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>带参数的invert位于base class SquareMatrixBase中，和SquareMatrix一样，SquareMatrixBase也是个template，不同的是它只对“矩阵元素对象的类型”参数化，不对矩阵的尺寸参数化，因此对于某给定之元素对象那个类型，所有矩阵共享同一个（也是唯一一个）SquareMatrixbase class，它们也将因此共享这唯一一个class内的invert。注意这些函数中我们使用“this-&gt;”标记，因为如果不这样做，便如条款43所讲的那样，模板化基类内的函数名称会被derived class掩盖，同时我们这儿使用private继承的原因是base class SquareMatrixBase只是为了帮助derived class SquareMatrix是实现，子类和父类之间不是“is-a”关系。</p> <p><strong>策略2：</strong>
针对SquareMatrixBase::invert的实现，我们该如何操作呢？解决办法是让SquareMatrixBase贮存一个指针，指向矩阵数值所在的内存，而只要它存储了这些东西，也就可能存储矩阵尺寸，如下：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>template &lt;typename T&gt;
class SquareMatrixBase{
protected:
    SquareMatrixBase(std::size_t n,T* pMem):size(n),pDate(pMem){

    }
    void setDataPtr(T* ptr){pData=ptr;}
    ...
private:
    std::size_t size;
    T* pData;
};
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>这允许derived class中决定内存分配方式，这里我们针对derived class有两种分配方式：
1）存储指针数组，这样可能导致对象吱声超级大！！！</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">T</span><span class="token punctuation">,</span>std<span class="token double-colon punctuation">::</span>size_t n<span class="token operator">&gt;</span>
<span class="token keyword">class</span> <span class="token class-name">SquareMatrix</span><span class="token operator">:</span><span class="token base-clause"><span class="token keyword">private</span> <span class="token class-name">SquareMatrixBase</span><span class="token operator">&lt;</span><span class="token class-name">T</span><span class="token operator">&gt;</span></span><span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token function">SquareMatrix</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span><span class="token generic-function"><span class="token function">SquareMtrixBase</span><span class="token generic class-name"><span class="token operator">&lt;</span>T<span class="token operator">&gt;</span></span></span><span class="token punctuation">(</span>n<span class="token punctuation">,</span>data<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token keyword">private</span><span class="token operator">:</span>
    T data<span class="token punctuation">[</span>n<span class="token operator">*</span>n<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>2）把每一个矩阵的数据放进heap中，即通过new进行动态内存分配；</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">T</span><span class="token punctuation">,</span>std<span class="token double-colon punctuation">::</span>size_t n<span class="token operator">&gt;</span>
<span class="token keyword">class</span> <span class="token class-name">SquareMatrix</span><span class="token operator">:</span><span class="token base-clause"><span class="token keyword">private</span> <span class="token class-name">SquareMatrixBase</span><span class="token operator">&lt;</span><span class="token class-name">T</span><span class="token operator">&gt;</span></span><span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token function">SquareMatrix</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span><span class="token generic-function"><span class="token function">SquareMatrixBase</span><span class="token generic class-name"><span class="token operator">&lt;</span>T<span class="token operator">&gt;</span></span></span><span class="token punctuation">(</span>n<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">pData</span><span class="token punctuation">(</span><span class="token keyword">new</span> T<span class="token punctuation">[</span>n<span class="token operator">*</span>n<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token operator">-&gt;</span><span class="token function">setDataPtr</span><span class="token punctuation">(</span>pData<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token keyword">private</span><span class="token operator">:</span>
    boost<span class="token double-colon punctuation">::</span>scoped_array<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span> pData<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><blockquote><p>1）non-type template parameters（非类型模板参数）：模板参数并不局限于类型，template &lt;&lt;&gt;typename T,int n&gt;或者template &lt;&lt;&gt;typename T,size_t n&gt;等等，第二个就是非类型模板参数！
2）type parameters：标准的类型模板参数，template &lt;&lt;&gt;typename T&gt;。</p></blockquote> <p>==<strong>请记住：</strong>==</p> <ol><li>==<strong>template生成多个classes和多个函数，所以任何template代码都不应该与某个造成膨胀的template参数产生相依关系；</strong>==</li> <li>==<strong>因类型模板参数（non-type template parameters）造成的代码膨胀，往往可以消除，做法是以函数参数或者class成员变量代替template参数；</strong>==</li> <li>==<strong>因类型参数（type parameters）而造成的代码膨胀，往往可以降低，做法是让带有完全相同的二进制表搜狐的具现类型共享实现码。</strong>==</li></ol> <h1 id="条款45-运用成员函数模板接受所有兼容类型"><a href="#条款45-运用成员函数模板接受所有兼容类型" class="header-anchor">#</a> 条款45：运用成员函数模板接受所有兼容类型</h1> <h2 id="一、添加成员函数模板"><a href="#一、添加成员函数模板" class="header-anchor">#</a> 一、添加成员函数模板</h2> <h3 id="以一个例子引出-何时设计成员函数模板"><a href="#以一个例子引出-何时设计成员函数模板" class="header-anchor">#</a> 以一个例子引出，何时设计成员函数模板</h3> <ul><li><strong>第一步：我们知道指针的一个特点就是：支持隐式转换</strong>。</li> <li>例如“指向 non-const 对象的指针可以转换为指向 const 对象”，“派生类指针可以隐式转换为基类指针”等等。代码如下：</li></ul> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">class</span> <span class="token class-name">Top</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">Middle</span> <span class="token operator">:</span><span class="token base-clause"><span class="token keyword">public</span> <span class="token class-name">Top</span></span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">Bottom</span> <span class="token operator">:</span><span class="token base-clause"><span class="token keyword">public</span> <span class="token class-name">Middle</span></span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
 
Top<span class="token operator">*</span> pt1 <span class="token operator">=</span> <span class="token keyword">new</span> Middle<span class="token punctuation">;</span>  <span class="token comment">//将Miffle*转换为Top*</span>
Top<span class="token operator">*</span> pt2 <span class="token operator">=</span> <span class="token keyword">new</span> Bottom<span class="token punctuation">;</span>  <span class="token comment">//将Bottom*转换为Top*</span>
<span class="token keyword">const</span> Top<span class="token operator">*</span> pct2 <span class="token operator">=</span> pt1<span class="token punctuation">;</span>  <span class="token comment">//将Top*转换为const Top*</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><ul><li>**第二步：**假设现在我们设计一个模板，用来模仿智能指针类，并且希望智能指针能像普通指针一样进行类型转换。例如：</li></ul> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">class</span> <span class="token class-name">Top</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">Middle</span> <span class="token operator">:</span><span class="token base-clause"><span class="token keyword">public</span> <span class="token class-name">Top</span></span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">Bottom</span> <span class="token operator">:</span><span class="token base-clause"><span class="token keyword">public</span> <span class="token class-name">Middle</span></span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
 
<span class="token comment">//自己设计的智能指针类</span>
<span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">T</span><span class="token operator">&gt;</span>
<span class="token keyword">class</span> <span class="token class-name">SmartPtr</span>
<span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token keyword">explicit</span> <span class="token function">SmartPtr</span><span class="token punctuation">(</span>T<span class="token operator">*</span> realPtr<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
 
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">//下面是我们希望能完成的，但是还没有实现</span>
    SmartPtr<span class="token operator">&lt;</span>Top<span class="token operator">&gt;</span> pt1 <span class="token operator">=</span> <span class="token generic-function"><span class="token function">SmartPtr</span><span class="token generic class-name"><span class="token operator">&lt;</span>Middle<span class="token operator">&gt;</span></span></span><span class="token punctuation">(</span><span class="token keyword">new</span> Middle<span class="token punctuation">)</span><span class="token punctuation">;</span>
    SmartPtr<span class="token operator">&lt;</span>Top<span class="token operator">&gt;</span> pt2 <span class="token operator">=</span> <span class="token generic-function"><span class="token function">SmartPtr</span><span class="token generic class-name"><span class="token operator">&lt;</span>Bottom<span class="token operator">&gt;</span></span></span><span class="token punctuation">(</span><span class="token keyword">new</span> Bottom<span class="token punctuation">)</span><span class="token punctuation">;</span>
    SmartPtr<span class="token operator">&lt;</span><span class="token keyword">const</span> Top<span class="token operator">&gt;</span> pct2 <span class="token operator">=</span> pt1<span class="token punctuation">;</span>
 
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><ul><li><p>第三步：根据上面的需求，我们希望让自己的智能指针类能像普通指针一样进行类型转换，那么我们可以为 SmartPtr 设计拷贝构造函数或拷贝赋值运算符，那么上面的功能就能实现了。</p> <ul><li>一种低效的做法是：在 SmartPtr 模板中针对于每一个 Top 派生类定义一个拷贝构造函数和拷贝赋值运算符。但是这种做法十分低效，因为针对每一个派生类设计相对应的拷贝构造函数和拷贝赋值运算符会使 class 膨胀，并且如果将来加入新的派生类，那么还需要继续添加新的成员函数</li></ul></li> <li><p>第四步：另一种做法是：为 SmartPtr <strong>模板添加一个成员函数模板</strong></p> <ul><li>例如：根据下面的拷贝构造函数，我们可以对任何类型T和任何类型U，将一个<code>SmartPtr&lt;U&gt;</code>转换为<code>SmartPtr&lt;T&gt;</code></li> <li>下面的拷贝构造函数并未声明为 explicit：因为原始指针类型之间的转换是隐式转换，如果我们的模板类型为原始指针，那么要支持这种隐式转换，因为我们并未声明 explicit</li></ul></li></ul> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">T</span><span class="token operator">&gt;</span>
<span class="token keyword">class</span> <span class="token class-name">SmartPtr</span>
<span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token comment">//拷贝构造函数，是一个成员函数模板</span>
    <span class="token keyword">typename</span><span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">U</span><span class="token operator">&gt;</span>
    <span class="token function">SmartPtr</span><span class="token punctuation">(</span><span class="token keyword">const</span> SmartPtr<span class="token operator">&lt;</span>U<span class="token operator">&gt;</span><span class="token operator">&amp;</span> other<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><ul><li>根据上面的介绍我们可以知道，为类模板设计一个成员函数模板是为了进行统一性与间接性，避免冗余操作</li></ul> <h2 id="二、约束成员函数模板的行为"><a href="#二、约束成员函数模板的行为" class="header-anchor">#</a> 二、约束成员函数模板的行为</h2> <ul><li><p>在“一”中，我们为智能指针类设计了拷贝构造函数，这样就可以根据类型进行类型转换了</p></li> <li><p><strong>但是还有一些问题没有解决：</strong></p> <ul><li>那就是，对于类继承来说，派生类指针可以转换为基类指针，但是基类指针不能转换为派生类指针</li> <li>类似的，对于普通类型来说，我们不能将<code>int*</code>转换为<code>double*</code></li></ul></li> <li><p>因此，即使我们设计了成员函数模板，那么<strong>还需要考虑一些转换的特殊情况</strong>（上面列出的）</p></li></ul> <h3 id="解决方法"><a href="#解决方法" class="header-anchor">#</a> 解决方法</h3> <blockquote><ul><li><p>我们可以为自己的只能指针类提供一个类似于shared_ptr的get()成员函数，这个函数返回智能指针锁封装的那个原始指针</p></li> <li><p><strong>设计的代码如下：</strong></p> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">T</span><span class="token operator">&gt;</span>
<span class="token keyword">class</span> <span class="token class-name">SmartPtr</span>
<span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token keyword">typename</span><span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">U</span><span class="token operator">&gt;</span>
    <span class="token function">SmartPtr</span><span class="token punctuation">(</span><span class="token keyword">const</span> SmartPtr<span class="token operator">&lt;</span>U<span class="token operator">&gt;</span><span class="token operator">&amp;</span> other<span class="token punctuation">)</span>
        <span class="token operator">:</span><span class="token function">heldPtr</span><span class="token punctuation">(</span>other<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    T <span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token keyword">const</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> heldPtr<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token keyword">private</span><span class="token operator">:</span>
    T<span class="token operator">*</span> heldPtr<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div></li> <li><p><strong>此处设计的原理：</strong></p> <ul><li>get()成员函数返回原始指针</li> <li>在拷贝构造函数中，我们使用了成员初始化列表来进行初始化智能指针封装的原始指针</li> <li>因此，在拷贝构造函数的构造过程中，是根据原始指针进行转换的，因此如果原始指针会自己判断这种转换行为：如果可以转换，那么拷贝构造函数就正确执行；如果不能转换，那么拷贝构造函数出错</li></ul></li></ul></blockquote> <h2 id="三、设计赋值成员函数模板"><a href="#三、设计赋值成员函数模板" class="header-anchor">#</a> 三、设计赋值成员函数模板</h2> <ul><li>我们上面设计的智能指针模板不限于构造函数，而且还可以自己设计赋值操作</li></ul> <h3 id="演示说明"><a href="#演示说明" class="header-anchor">#</a> 演示说明</h3> <blockquote><ul><li><strong>例如shared_ ptr：</strong> <ul><li>支持所有“来自内置指针、shared_ptr、auto_ptr、weak_ptr”的构造函数</li> <li>支持上面所有（出去weak_ptr）的赋值操作</li></ul></li> <li><strong>例如下面是shared_ptr的源码摘录：</strong>（其中template参数强烈倾向使用class而不是typename）</li></ul> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">T</span><span class="token operator">&gt;</span>
<span class="token keyword">class</span> <span class="token class-name">shared_ptr</span>
<span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token comment">//下面都是拷贝构造函数(列出了一部分)</span>
    <span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">Y</span><span class="token operator">&gt;</span>
    <span class="token keyword">explicit</span> <span class="token function">shared_ptr</span><span class="token punctuation">(</span>Y<span class="token operator">*</span> p<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">Y</span><span class="token operator">&gt;</span>
    <span class="token function">shared_ptr</span><span class="token punctuation">(</span>shared_ptr<span class="token operator">&lt;</span>Y<span class="token operator">&gt;</span> <span class="token keyword">const</span><span class="token operator">&amp;</span> r<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">Y</span><span class="token operator">&gt;</span>
    <span class="token keyword">explicit</span> <span class="token function">shared_ptr</span><span class="token punctuation">(</span>weak_ptr<span class="token operator">&lt;</span>Y<span class="token operator">&gt;</span> <span class="token keyword">const</span><span class="token operator">&amp;</span> r<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">Y</span><span class="token operator">&gt;</span>
    <span class="token keyword">explicit</span> <span class="token function">shared_ptr</span><span class="token punctuation">(</span>auto_ptr<span class="token operator">&lt;</span>Y<span class="token operator">&gt;</span><span class="token operator">&amp;</span> r<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//下面都是赋值操作(列出了一部分)</span>
    <span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">Y</span><span class="token operator">&gt;</span>
    shared_ptr<span class="token operator">&amp;</span> <span class="token keyword">operator</span><span class="token operator">=</span><span class="token punctuation">(</span>shared_ptr<span class="token operator">&lt;</span>Y<span class="token operator">&gt;</span> <span class="token keyword">const</span><span class="token operator">&amp;</span> r<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">Y</span><span class="token operator">&gt;</span>
    shared_ptr<span class="token operator">&amp;</span> <span class="token keyword">operator</span><span class="token operator">=</span><span class="token punctuation">(</span>auto_ptr<span class="token operator">&lt;</span>Y<span class="token operator">&gt;</span><span class="token operator">&amp;</span> r<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><ul><li><strong>代码说明：</strong> <ul><li>构造函数：都是explicit，除了“泛化copy构造函数”除外。因为从某个shared_ptr类型隐式转换为另一个shared_ptr是被允许的，但是从某个内置指针或从其他智能指针进行隐式转换为shared_ptr是不被允许的（除了使用cast进行强制类型转换）</li> <li>auto_ptr：参数为auto_ptr的拷贝构造函数和赋值运算符，其参数都不是const的（条款13说过，当你赋值一个auto_ptr时，我们希望其所管理的对象被移动改动）</li></ul></li></ul></blockquote> <h2 id="四、与默认函数的区别"><a href="#四、与默认函数的区别" class="header-anchor">#</a> 四、与默认函数的区别</h2> <ul><li>我们曾说过，一个类如果没有提供构造函数、拷贝构造函数、拷贝赋值运算符，那么编译器会自动为类提供合成/默认的版本，这一规则同样适用于模板类</li> <li>因此，例如我们上面为自己的类添加了成员函数模板（拷贝构造函数），那么当我们使用拷贝构造函数的时候是调用哪一个版本呢？答案为：根据实际调用情况选择</li> <li>因此，如果我们自己设计成员函数模板还需要拷贝类为我们自己提供的合成版本，在必要时自己设计非成员函数模板</li> <li>例如，下面是shared_ptr的源码摘录：</li></ul> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">T</span><span class="token operator">&gt;</span>
<span class="token keyword">class</span> <span class="token class-name">shared_ptr</span>
<span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token comment">//拷贝构造函数</span>
    <span class="token function">shared_ptr</span> <span class="token punctuation">(</span>shared_ptr <span class="token keyword">const</span><span class="token operator">&amp;</span> r<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//非泛化版本</span>
    <span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">Y</span><span class="token operator">&gt;</span>
    <span class="token function">shared_ptr</span><span class="token punctuation">(</span>shared_ptr<span class="token operator">&lt;</span>Y<span class="token operator">&gt;</span> <span class="token keyword">const</span><span class="token operator">&amp;</span> r<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//泛化版本</span>
 
    <span class="token comment">//拷贝赋值运算符</span>
    shared_ptr<span class="token operator">&amp;</span> <span class="token keyword">operator</span><span class="token operator">=</span><span class="token punctuation">(</span>shared_ptr <span class="token keyword">const</span><span class="token operator">&amp;</span> r<span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">//非泛化版本</span>
    <span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">Y</span><span class="token operator">&gt;</span>
    shared_ptr<span class="token operator">&amp;</span> <span class="token keyword">operator</span><span class="token operator">=</span><span class="token punctuation">(</span>shared_ptr<span class="token operator">&lt;</span>Y<span class="token operator">&gt;</span> <span class="token keyword">const</span><span class="token operator">&amp;</span> r<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//泛化版本</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h2 id="五、总结"><a href="#五、总结" class="header-anchor">#</a> 五、总结</h2> <p>==<strong>请记住：</strong>==</p> <ul><li>==<strong>请使用member function templates（成员函数模板）生成“可接受所有兼容类型”的函数</strong>==</li> <li>==<strong>如果你生命member templates用于“泛化copy构造”或“泛化assignment操作”，你还是需要声明正常的copy构造函数和copy assignment操作符</strong>==</li></ul> <h1 id="条款46-需要类型转换时请为模板定义非成员函数"><a href="#条款46-需要类型转换时请为模板定义非成员函数" class="header-anchor">#</a> 条款46：需要类型转换时请为模板定义非成员函数</h1> <p>首先为什么类似于operator 这样的重载运算符要定义成非成员函数，主要是为了保证混合乘法2 Variant或者Variant * 2都可以通过编译，但是2不能同时进行隐式类型转换成某个Variant，再作this用。</p> <p>所以我们一般将之定义成友元函数，像下面这样：</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">class</span> <span class="token class-name">Variant</span>
<span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token function">Variant</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">int</span><span class="token operator">&amp;</span> value <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
 
    <span class="token keyword">friend</span> <span class="token keyword">const</span> Variant <span class="token keyword">operator</span><span class="token operator">*</span> <span class="token punctuation">(</span><span class="token keyword">const</span> Variant<span class="token operator">&amp;</span>lhs<span class="token punctuation">,</span> <span class="token keyword">const</span> Variant<span class="token operator">&amp;</span> rhs<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
 
<span class="token keyword">const</span> Variant <span class="token keyword">operator</span><span class="token operator">*</span> <span class="token punctuation">(</span><span class="token keyword">const</span> Variant<span class="token operator">&amp;</span>lhs<span class="token punctuation">,</span> <span class="token keyword">const</span> Variant<span class="token operator">&amp;</span> rhs<span class="token punctuation">)</span> 
<span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">Variant</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
 
<span class="token comment">//使用方式</span>
Variant <span class="token function">val</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>             <span class="token comment">//ok</span>
Variant result <span class="token operator">=</span> val <span class="token operator">*</span> <span class="token number">1</span><span class="token punctuation">;</span>   <span class="token comment">//ok</span>
result <span class="token operator">=</span> <span class="token number">2</span> <span class="token operator">*</span> val<span class="token punctuation">;</span>           <span class="token comment">//ok</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><p>现在引入模板，可以像下面这样写，注意这里的operator*是一个独立的模板函数：</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">T</span><span class="token operator">&gt;</span>
<span class="token keyword">class</span> <span class="token class-name">Variant</span>
<span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token function">Variant</span><span class="token punctuation">(</span><span class="token keyword">const</span> T<span class="token operator">&amp;</span> value <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
 
<span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">T</span><span class="token operator">&gt;</span>
<span class="token keyword">const</span> Variant<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span> <span class="token keyword">operator</span><span class="token operator">*</span> <span class="token punctuation">(</span><span class="token keyword">const</span> Variant<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span><span class="token operator">&amp;</span>lhs<span class="token punctuation">,</span> <span class="token keyword">const</span> Variant<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span><span class="token operator">&amp;</span> rhs<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token generic-function"><span class="token function">Variant</span><span class="token generic class-name"><span class="token operator">&lt;</span>T<span class="token operator">&gt;</span></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>但是这次不能通过编译并且在vs2015下的报错信息：</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code>C2784   “<span class="token keyword">const</span> Variant<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span> <span class="token keyword">operator</span> <span class="token operator">*</span><span class="token punctuation">(</span><span class="token keyword">const</span> Variant<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span> <span class="token operator">&amp;</span><span class="token punctuation">,</span><span class="token keyword">const</span> Variant<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span> <span class="token operator">&amp;</span><span class="token punctuation">)</span>”<span class="token operator">:</span> 未能从“<span class="token keyword">int</span>”为“<span class="token keyword">const</span> Variant<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span> <span class="token operator">&amp;</span>”推导 模板 参数 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>原因是编译器推导T出现了困难，val * 1在编译器看来，可以由a是Variant将T推导成int，但是1是什么，理想情况下编译器会尝试将它先转换成一个Variant，并将T推导成int，但事实上编译器在“T推导过程中从不将隐式类型转换函数纳入考虑”。所以无论是val * 1还是1 * val 都是不能通过编译的，一句话，隐式转换+推导T不能被同时被编译器接受。</p> <p>解决问题的思路便接着产生，编译器既然不能同时接受这两个过程，就让它们事先满足好一个条件，再由编译器执行另一个过程好了。</p> <p>如果把这个operator*放在template class里面，也就是先在生成模板类的那一步就定下T，这样编译器只要执行隐式转换这一步就可以了。</p> <p>因此我们可以这样来改：</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">T</span><span class="token operator">&gt;</span>
<span class="token keyword">class</span> <span class="token class-name">Variant</span>
<span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token function">Variant</span><span class="token punctuation">(</span><span class="token keyword">const</span> T<span class="token operator">&amp;</span> value <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
 
    <span class="token keyword">friend</span> <span class="token keyword">const</span> Variant<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span> <span class="token keyword">operator</span><span class="token operator">*</span> <span class="token punctuation">(</span><span class="token keyword">const</span> Variant<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span><span class="token operator">&amp;</span>lhs<span class="token punctuation">,</span> <span class="token keyword">const</span> Variant<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span><span class="token operator">&amp;</span> rhs<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>在类中加一个友元。</p> <p>我们添加了一个友元函数的声明，果然编译通过了，但链接时又报错了，<strong>原因是链接器找不到operator*的定义，这里又要说模板类中的一个特殊情况了，它不同与普通的类，模板类的友元函数只能在类中实现，所以要把函数体部分移至到类内，像下面这样：</strong></p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">T</span><span class="token operator">&gt;</span>
<span class="token keyword">class</span> <span class="token class-name">Variant</span>
<span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token function">Variant</span><span class="token punctuation">(</span><span class="token keyword">const</span> T<span class="token operator">&amp;</span> value <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
 
    <span class="token keyword">friend</span> <span class="token keyword">const</span> Variant<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span> <span class="token keyword">operator</span><span class="token operator">*</span> <span class="token punctuation">(</span><span class="token keyword">const</span> Variant<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span><span class="token operator">&amp;</span>lhs<span class="token punctuation">,</span> <span class="token keyword">const</span> Variant<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span><span class="token operator">&amp;</span> rhs<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token generic-function"><span class="token function">Variant</span><span class="token generic class-name"><span class="token operator">&lt;</span>T<span class="token operator">&gt;</span></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>这下编译和链接都没有问题了。这里还要说一下，就是移至类内后，T的标识符可以不写了。</p> <p>operator<em>里面只有一句话，但如果friend函数里面的东西太多了，可以定义一个辅助方法，比如Doxxx()，这个Doxxx可以放在类外去实现，Doxxx本身不支持混合乘法（2 * Variant或者Variant</em> 2），但由于在operator*里面已经进行了隐式类型转换，所以到Doxxx()这一级是没有问题的。</p> <p>==<strong>请记住：</strong>==</p> <ul><li>==<strong>当我们编写一个class template，而它所提供之“与此template相关的”函数支持“所有参数之隐式类型转换”时，请将那些函数定义为“class template内部的friend函数”。</strong>==</li></ul> <h1 id="条款47-请使用traits-classes表现类型信息"><a href="#条款47-请使用traits-classes表现类型信息" class="header-anchor">#</a> 条款47:请使用traits classes表现类型信息</h1> <h2 id="_1、如果想写一个将迭代器移动d单位的函数"><a href="#_1、如果想写一个将迭代器移动d单位的函数" class="header-anchor">#</a> 1、如果想写一个将迭代器移动d单位的函数</h2> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">template</span> IterT<span class="token punctuation">,</span><span class="token keyword">typename</span> <span class="token class-name">DistT</span><span class="token operator">&gt;</span>
<span class="token keyword">void</span> <span class="token function">advance</span><span class="token punctuation">(</span>IterT<span class="token operator">&amp;</span> iter<span class="token punctuation">,</span>DistT d<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//d&gt;0前移，d&lt;0后移</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>具体过程该怎么写呢？直接写 iter += d 是行不通的，因为这种只有random acces（随机访问）迭代器才支持+=的操作，而其他威力不那么强大的迭代器种类，advance必须反复施行 ++ 或 --，共d次。</p> <h2 id="_2、stl迭代器一共有5类"><a href="#_2、stl迭代器一共有5类" class="header-anchor">#</a> 2、STL迭代器一共有5类</h2> <ul><li><p>input迭代器：**只能向前移动，一次一步，只能读取。**例如，模仿指向输入文件的读指针(read pointer)。Cpp程序库中的<code>istream_iterators</code>是这一分类的代表。</p></li> <li><p>Output迭代器：**只能向前移动，一次一步，只能写。**例如，模仿指向输出文件的写指针(write pointer)。Cpp程序库中的<code>ostream_iterators</code>是这一分类的代表。</p></li></ul> <blockquote><p>以上两种迭代器是威力最小的两个迭代器分类。因为一次只能一步，所以它们只适合“一次性操作算法”</p></blockquote> <ul><li><p>forward迭代器：<strong>可以做前述两种迭代器所能做的每一件事，而且可以读写其所指物一次以上，<strong>因此可施行于“多次性操作算法”，TR1 hashed容器可能是这一分类</strong>（这里说“可能”是因为hashed容器的迭代器可为单向也可为双向，取决于实现版本）。</strong></p></li> <li><p>Bidirectional迭代器：**除了可前移还可后移。**STL的list迭代器就属于这一分类，set，multiset，map和multimap的迭代器也都是这一分类。</p></li> <li><p>random access迭代器：**威力最大的迭代器，可以执行“迭代器算术”，因此可在常量时间内前后随机移动任意的距离。**vector，deque和string提供的迭代器都是这一分类。有时内置指针也可被当做random access 迭代器使用。</p></li></ul> <p>对于这5类，Cpp标准库分别提供tag struct（卷标结构）加以确认</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">struct</span> <span class="token class-name">input_iterator_tag</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">struct</span> <span class="token class-name">output_iterator_tag</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">struct</span> <span class="token class-name">forward_iterator_tag</span> <span class="token operator">:</span><span class="token base-clause"><span class="token keyword">public</span> <span class="token class-name">input_iterator_tag</span></span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">struct</span> <span class="token class-name">bidirectional_iterator_tag</span> <span class="token operator">:</span><span class="token base-clause"><span class="token keyword">public</span> <span class="token class-name">forward_iterator_tag</span></span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">struct</span> <span class="token class-name">random_access_iterator_tag</span> <span class="token operator">:</span><span class="token base-clause"><span class="token keyword">public</span> <span class="token class-name">bidirectional_iterator_tag</span></span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>在写advance函数时前4类迭代器可以使用++，--，而最后一个可以使用iter+=d,所以我们就需要在编译期取得类型信息，知道是哪一种迭代器.</p> <h2 id="_3、解决方式就是利用iterator-traits"><a href="#_3、解决方式就是利用iterator-traits" class="header-anchor">#</a> 3、解决方式就是利用iterator_traits</h2> <p><code>template&lt;typename IterT&gt; struct iterator_traits;</code>//template,用来处理迭代器分类的相关信息</p> <p>iterator_traits的运作方式是，针对每一个IterT,在<code>struct iterator_traits&lt;IterT&gt;</code>内一定声明某个typedef名为<code>iterator_category</code>。这个typedef用来确认IterT的迭代器分类。即首先要求用户自定义的迭代器类型必须嵌套一个typedef，名为<code>iterator_category</code>,用来确认适当的卷标结构</p> <p>例如</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token operator">&gt;</span>
<span class="token keyword">class</span> <span class="token class-name">deque</span><span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
	<span class="token keyword">class</span> <span class="token class-name">iterator</span><span class="token punctuation">{</span>
	<span class="token keyword">public</span><span class="token operator">:</span>
		<span class="token keyword">typedef</span> random_access_iterator_tag iterator_category<span class="token punctuation">;</span>
	<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>iterator_traits只是响应iterator class的嵌套式typedef</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">IterT</span><span class="token operator">&gt;</span>
<span class="token keyword">struct</span> <span class="token class-name">iterator_traits</span><span class="token punctuation">{</span>
	<span class="token keyword">typedef</span> <span class="token keyword">typename</span> <span class="token class-name">IterT</span><span class="token double-colon punctuation">::</span>iterator_category iterator_category<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token comment">//针对指针,偏特化</span>
<span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">IterT</span><span class="token operator">&gt;</span>
<span class="token keyword">struct</span> <span class="token class-name">iterator_traits</span><span class="token operator">&lt;</span>IterT<span class="token operator">*</span><span class="token operator">&gt;</span><span class="token punctuation">{</span>
	<span class="token keyword">typedef</span> <span class="token keyword">typename</span> <span class="token class-name">IterT</span><span class="token double-colon punctuation">::</span>iterator_category iterator_category<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>迭代器移动函数可这样实现</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">IterT</span><span class="token punctuation">,</span><span class="token keyword">typename</span> <span class="token class-name">DistT</span><span class="token operator">&gt;</span>
<span class="token keyword">void</span> <span class="token function">doAdvance</span><span class="token punctuation">(</span>IterT<span class="token operator">&amp;</span> iter<span class="token punctuation">,</span> DistT d<span class="token punctuation">,</span> std<span class="token double-colon punctuation">::</span>bidirectional_iterator_tag<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span>d <span class="token operator">&gt;=</span> <span class="token number">0</span><span class="token punctuation">)</span> 
	<span class="token punctuation">{</span>
		<span class="token keyword">while</span> <span class="token punctuation">(</span>d<span class="token operator">--</span><span class="token punctuation">)</span>
			<span class="token operator">++</span>iter<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">else</span>
	<span class="token punctuation">{</span>
		<span class="token keyword">while</span> <span class="token punctuation">(</span>d<span class="token operator">++</span><span class="token punctuation">)</span>
			<span class="token operator">--</span>iter<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">IterT</span><span class="token punctuation">,</span> <span class="token keyword">typename</span> <span class="token class-name">DistT</span><span class="token operator">&gt;</span>
<span class="token keyword">void</span> <span class="token function">doAdvance</span><span class="token punctuation">(</span>IterT<span class="token operator">&amp;</span> iter<span class="token punctuation">,</span> DistT d<span class="token punctuation">,</span> std<span class="token double-colon punctuation">::</span>input_iterator_tag<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span>d <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span>
		<span class="token keyword">throw</span> std<span class="token double-colon punctuation">::</span><span class="token function">out_of_range</span><span class="token punctuation">(</span><span class="token string">&quot;Negative distance&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span>d<span class="token operator">--</span><span class="token punctuation">)</span> <span class="token operator">++</span>iter<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">IterT</span><span class="token punctuation">,</span> <span class="token keyword">typename</span> <span class="token class-name">DistT</span><span class="token operator">&gt;</span>
<span class="token keyword">void</span> <span class="token function">advance</span><span class="token punctuation">(</span>IterT<span class="token operator">&amp;</span> iter<span class="token punctuation">,</span> DistT d<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token function">doAdvance</span><span class="token punctuation">(</span>iter<span class="token punctuation">,</span> d<span class="token punctuation">,</span> <span class="token keyword">typename</span> <span class="token class-name">std</span><span class="token double-colon punctuation">::</span>iterator_traits<span class="token operator">&lt;</span>IterT<span class="token operator">&gt;</span><span class="token double-colon punctuation">::</span>iterator_category<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div><p>==<strong>请记住</strong>==</p> <ul><li>==<strong>Traits classes使得“类型相关信息”在编译期可用。它们以templates和“templates特化”完成实现</strong>==</li> <li>==<strong>整合重载技术后，traits classes有可能在编译期对类型执行if...else测试</strong>==</li></ul> <h1 id="条款48-认识template元编程"><a href="#条款48-认识template元编程" class="header-anchor">#</a> 条款48：认识template元编程</h1> <h2 id="_1、内容"><a href="#_1、内容" class="header-anchor">#</a> 1、内容</h2> <ol><li><p>template metaprogramming（TMP，模板元编程）是：编写 template Cpp 程序并执行于编译期的过程。所谓模板元程序就是：以 Cpp 写成，执行于 Cpp 编译器内的程序。该程序执行后产生具现的代码，和正常代码一并加入编译。也就说元编程可以做到用代码去生成代码。</p></li> <li><p>TMP 伟大之处在于，由于 template metaprograms 执行于 Cpp 编译期，因此可以将很多工作从运行期转移到编译期。例如：</p> <ul><li><p>某些错误原本通常在运行期才能检测到，现在可在编译器找出来。</p></li> <li><p>使用 TMP 的 Cpp 程序可能在每一方面都更加高效：比如较小的可执行文件，较短的运行期，较少的内存需求。</p></li></ul></li> <li><p>在条款 47 我们曾提到如何实现一个 Move 函数：</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">IteratorType</span><span class="token operator">&gt;</span>
<span class="token keyword">void</span> <span class="token function">Move</span><span class="token punctuation">(</span>IteratorType<span class="token operator">&amp;</span> Iterator<span class="token punctuation">,</span> <span class="token keyword">int</span> Distance<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 使用类型信息</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">typeid</span><span class="token punctuation">(</span>IteratorTraits<span class="token operator">&lt;</span>IteratorType<span class="token operator">&gt;</span><span class="token double-colon punctuation">::</span>IteratorTag<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token keyword">typeid</span><span class="token punctuation">(</span>RandomAccessIteratorTag<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>   
        Iterator <span class="token operator">+=</span> Distance<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>Distance <span class="token operator">&gt;=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">while</span> <span class="token punctuation">(</span>Distance<span class="token operator">--</span><span class="token punctuation">)</span><span class="token operator">++</span>Iterator<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token keyword">while</span> <span class="token punctuation">(</span>Distance<span class="token operator">++</span><span class="token punctuation">)</span><span class="token operator">--</span>Iterator<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>并且我们曾描述这可能存在编译问题，现在就让我们看看是什么：虽然我们这里根据迭代器类型进行不同的操作，或 +=，或 ++，–，我们知道只有 Random Access Iterator 可以有 += 运算，但是 Cpp 要求：编译器必须确保所有源码都有效，即使是不会执行的源码。也就是说编译器会拿着其他不支持 += 的迭代器，进入 if 语句先测试是否支持 += 运算，无效则会报错。</p> <p>所以相比于要支持所有操作，Traits class 针对不同类型进行函数重载的做法显然更好。</p></li> <li><p>我们先简单了解一下 TMP 编程。TMP 已被证明是一个图灵完备（Turing-complete）机器，这意味着它可以计算任何事物，使用 TMP 你可以声明变量，执行循环，编写及调用函数…但这些相对于正常的 Cpp 的实现会有很大的不同。比如：TMP 并没有循环部件，所有的循环效果都由递归完成。如果你不了解递归，恐怕必须先解决这个问题。</p></li> <li><p>一个经典的入门案例是使用 TMP 计算阶乘：</p> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">unsigned</span> N<span class="token operator">&gt;</span>
<span class="token keyword">struct</span> <span class="token class-name">Factorial</span> <span class="token punctuation">{</span>
    <span class="token keyword">static</span> <span class="token keyword">const</span> <span class="token keyword">int</span> Value <span class="token operator">=</span> N <span class="token operator">*</span> Factorial<span class="token operator">&lt;</span>N <span class="token operator">-</span> <span class="token number">1</span><span class="token operator">&gt;</span><span class="token double-colon punctuation">::</span>Value<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token operator">&gt;</span>
<span class="token keyword">struct</span> <span class="token class-name">Factorial</span><span class="token operator">&lt;</span><span class="token number">0</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">static</span> <span class="token keyword">const</span> <span class="token keyword">int</span> Value <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">inline</span> <span class="token keyword">void</span> <span class="token function">TryWithFactorial</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    std<span class="token double-colon punctuation">::</span>cout <span class="token operator">&lt;&lt;</span> Factorial<span class="token operator">&lt;</span><span class="token number">10</span><span class="token operator">&gt;</span><span class="token double-colon punctuation">::</span>Value <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;\n&quot;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><p>和所有递归行为一样，我们需要一个特殊情况来结束递归。对于 TMP 而言就是使用 template 的特化版本。</p> <p>正如 TryWithFactorial 函数所使用的，只要你声明<code>Factorial&lt;N&gt;::Value</code> 就可以得到 N 阶乘值。当然这里存在值溢出的问题。</p></li> <li><p>TMP 目前是一个新生的技术，语法不直观，也缺少 template 相关的调试器，但它将运行期工作移至编译期所带来的效率提升还是很令人印象深刻。</p></li></ol> <h2 id="_2、-总结"><a href="#_2、-总结" class="header-anchor">#</a> 2、 总结</h2> <p>==<strong>请记住：</strong>==</p> <ul><li>==<strong>Template metaprogramming（TMP，模板元编程）可将工作由运行期移往编译期，因而得以实现早期错误侦测和更高的执行效率。</strong>==</li> <li>==<strong>TMP 可被用来生成基于政策选择组合（based on combination of policy choices）的客户定制代码，也可用来避免生成对某些特殊类型并不合适的代码。</strong>==</li></ul></div></div> <!----> <div class="page-edit"><!----> <!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2024/6/3 14:54:44</span></div></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/b4a7fb/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">条款32-40 继承与面向对象设计</div></a> <a href="/pages/6b2468/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">第4章 Cpp多线程系统编程精要</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/b4a7fb/" class="prev">条款32-40 继承与面向对象设计</a></span> <span class="next"><a href="/pages/6b2468/">第4章 Cpp多线程系统编程精要</a>→
      </span></p></div></div></div> <!----></main></div> <div class="footer"><!----> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2023-2025
    <span>霜晨月</span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><canvas id="vuepress-canvas-cursor"></canvas></div></div>
    <script src="/assets/js/app.67adcfd9.js" defer></script><script src="/assets/js/4.9aaa1650.js" defer></script><script src="/assets/js/1.5474518c.js" defer></script><script src="/assets/js/3.593d14fc.js" defer></script><script src="/assets/js/58.aad57f31.js" defer></script>
  </body>
</html>
